<?php

require __DIR__ . '/../vendor/autoload.php';

use Web3\Contract;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Web3;

$url = 'https://ropsten.infura.io/v3/8f762549c8c341388ac03552835a0358';
$web3 = new Web3(new HttpProvider(new HttpRequestManager($url, 15)));

$account1 = '0x9Ba6EC8fA4CD0AC4b805c605cEBE4901046c4041';
$account2 = '0xDF7C5453DFFf67579cF74A148424fE2d366E21C3';

$privateKey1 = '85505aa1c1b634c60a24f94dd7209fa8bda2fd46179b15080eff427b0b320a85';
$privateKey2 = 'b109c576f6385f7f740dbae9c12dd109a4f47877a58ae0e738e6ac669dffdf75';

// 读取已部署的契约 -- 从Etherscan获取地址
$contract_address = '0x8125C32ba2ADcc85DA3a1832975111c2C55C15e3';
$contract_abi = '
[
  {
    "inputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "constructor"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "get",
    "outputs": [
      {
        "internalType": "string",
        "name": "",
        "type": "string"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "internalType": "string",
        "name": "_value",
        "type": "string"
      }
    ],
    "name": "set",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  }
]
';

$contract = new Contract($web3->getProvider(), $contract_abi);
$contract->at($contract_address)->call('get', function ($err, $res) {
    if ($err !== null) {
        echo 'Error: ' . $err->getMessage() . PHP_EOL;
        return;
    }

    var_dump($res);
});